import { Hook } from "./Hook";
import { GameManager } from "./GameManager";
import { MineItem } from "./MineItem";
import { ShopItemManager } from "./ShopItemManager";

const { ccclass, property } = cc._decorator;

@ccclass
export class HookCollider extends cc.Component {

    hookScript: Hook; //钩子脚本

    other: cc.Node;

    @property(cc.Prefab)
    explodePrefab;

    @property(cc.Prefab)
    endExplodePaticle;

    start() {

    }

    onLoad() {
        cc.director.getCollisionManager().enabled = true;
        var manager = cc.director.getCollisionManager();  // 获取碰撞检测类
        manager.enabled = true   //开启碰撞检测
        //manager.enabledDebugDraw=true //显示碰撞检测区域
        this.hookScript = this.node.parent.getComponent(Hook);
    }

    onCollisionEnter(other, self) {
        if (other.node.group == 'tnt') {
            let tmp = cc.instantiate(this.endExplodePaticle);
            tmp.parent = this.node;
            tmp.setPosition(cc.v2(0, 0));
            other.node.destroy();
            setTimeout(function () {
                GameManager.Instance(GameManager).endGamePanel.active = true;
            }, 1000)
            return;
        }
        if (other.node.group == "wall") {
            this.hookScript.ropeState = 2;
        } else {
            if (!GameManager.Instance(GameManager).catchItem) {
                GameManager.Instance(GameManager).catchItem = other.node.getComponent(MineItem);
                GameManager.Instance(GameManager).ropeSpeed = GameManager.Instance(GameManager).catchItem.speed;
                other.node.parent = this.node;
                this.other = other;
                other.node.x = 0;
                other.node.y = 0;
            }
        }
    }

    /**
     * 炸掉拉到的东西
     */
    public explodeThings() {
        let gameManager = GameManager.Instance(GameManager);
        gameManager.catchItem = null;
        ShopItemManager.Instance(ShopItemManager).reduceBombCount();
        ShopItemManager.Instance(ShopItemManager).refreshDisplay();
        this.node.removeAllChildren();
        let particle = cc.instantiate(this.explodePrefab);
        particle.parent = this.node;
        particle.setPosition(cc.v2(0, 0))
        gameManager.ropeSpeed = gameManager.normalRopeSpeed;
        gameManager.ropeState = gameManager.ROPE_STATE.REDUCE;
    }
}
